/**   
 * @Title: BindMobile_InputCodeAct.java 
 * @Package: com.esun.rabbit2.viewer.mainact.mylotteryact.bindmobile 
 * @Description: 输入验证码
 * @Author: Miko baoz@500wan.com   
 * @Date: 2011-7-9 下午04:47:22  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.bindmobile;

import com.esun.config.Language;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.myinfo.SendCodeCommunicater;
import com.esun.ui.R;
import com.esun.util.EditTextInputUitl;
import com.esun.util.ViewerUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * @ClassName: BindMobile_InputCodeAct
 * @Description: 输入验证码
 * @Author: Miko baoz@500wan.com
 * @Date: 2011-7-9 下午04:47:22
 * 
 */
public class BindMobile_InputCodeAct extends Activity implements OnClickListener {

	private Button btnOK;

	private EditText etCode;
	private Button btnCodeClear;

	private String strVerifyCode;
	private String strNew;

	private ImageButton btnGoBack;

	private Handler handler;

	private ProgressDialog progress;

	private static final int SEND_CODE_SUCCESS = 100003;
	private static final int SEND_CODE_FAILED = 100004;

	SendCodeCommunicater sendCode = new SendCodeCommunicater();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bind_mobile_input_code);
		init();
		initHandler();
	}

	private void init() {

		btnGoBack = (ImageButton) this.findViewById(R.id.bind_mobile_input_code_btn_goBack);
		btnGoBack.setOnClickListener(this);

		strNew = this.getIntent().getStringExtra("new");

		etCode = (EditText) this.findViewById(R.id.bind_mobile_input_code_et_code);
		btnCodeClear = (Button) this.findViewById(R.id.bind_mobile_input_code_et_code_btn_claen);
		etCode = new EditTextInputUitl(etCode, btnCodeClear).getEditText();

		btnOK = (Button) this.findViewById(R.id.bind_mobile_input_code_btn_ok);
		btnOK.setOnClickListener(this);

		progress = ViewerUtil.showProgressDialog(this, "请稍候...");
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.dismiss();
				switch (msg.what) {

				case SEND_CODE_SUCCESS:

					BindMobile_Bind.isBindOK = true;
					ViewerUtil.showTipDialog(BindMobile_InputCodeAct.this, "手机号码修改成功！", true);

					break;

				case SEND_CODE_FAILED:

					BindMobile_Bind.isBindOK = false;
					//ViewerUtil.showTipDialog(BindMobile_InputCodeAct.this, Language.BINDING_MOBEL_FAIL, false);
					Toast.makeText(BindMobile_InputCodeAct.this, Language.STR_CONNECTIONFAIL.toString(), 0).show();
					break;

				default:
					break;
				}
			}
		};
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bind_mobile_input_code_btn_ok:

			strVerifyCode = etCode.getText().toString();

			if (strVerifyCode.length() > 0) {
				progress.show();
				startSendCode();
			} else {
				ViewerUtil.showTipDialog(this, "请正确输入验证码", false);
			}

			break;

		case R.id.bind_mobile_input_code_btn_goBack:
			this.finish();
			break;

		default:
			break;
		}
	}

	private void startSendCode() {
		new Thread() {

			@Override
			public void run() {
				super.run();
				doSendCode();
			}
		}.start();
	}

	private void doSendCode() {

		if (sendCode.sendCode(strVerifyCode, strNew)) {
			Message msg = handler.obtainMessage(SEND_CODE_SUCCESS);
			handler.sendMessage(msg);
		} else {
			Message msg = handler.obtainMessage(SEND_CODE_FAILED);
			handler.sendMessage(msg);
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		 this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}
}
